package org.example.service.Impl;

import org.example.mapper.FoodMapper;
import org.example.mapper.FoodTagMapper;
import org.example.pojo.Food;
import org.example.pojo.FoodTag;
import org.example.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {  // 添加 implements FoodService
    private final FoodMapper foodMapper;
    private final FoodTagMapper foodTagMapper;

    @Autowired
    public FoodServiceImpl(FoodMapper foodMapper, FoodTagMapper foodTagMapper) {
        this.foodMapper = foodMapper;
        this.foodTagMapper = foodTagMapper;
    }

    // 查询所有
    @Override
    public List<Food> findAll() {
        return foodMapper.findAll();
    }

    // 根据id查询
    @Override  // 修正方法名与接口一致
    public Food findById(Long id) {  // 原getFoodById改为findById
        return foodMapper.findById(id);  // 确保Mapper方法名也同步修改
    }

    @Override
    public Food addFood(Food food) {
        foodMapper.addFood(food);
        return food;
    }

    @Override
    public void deleteById(Long id) {
        // 建议先删除关联标签
        foodTagMapper.deleteByFoodId(id);
        foodMapper.deleteById(id);
    }

    @Override
    public List<FoodTag> findTagsByFoodId(Long id) {
        return foodTagMapper.findTagsByFoodId(id);
    }

    @Override
    public FoodTag addTag(FoodTag tag) {
        foodTagMapper.addTag(tag);
        return tag;
    }
}